package org.example.controller;

import jakarta.annotation.Resource;
import org.example.pojo.*;
import org.example.service.UserControlService;
import org.example.service.UserService;
import org.example.utils.JwtToken;
import org.example.utils.LoginUserResponseData;
import org.example.utils.R;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 用户管理模块controller
 * @Author: 曹震
 * @Date: 2024/11/29 19:06
 */
@CrossOrigin // 允许其他源访问我们的controller
@RestController
@RequestMapping("userControl")
public class UserController {

    @Resource
    private UserControlService sevice;

    @Resource
    private UserService userService;

    // 查找志愿者注册状态在待审核的志愿者信息
    @GetMapping(value = "check")
    public R checkVolunteer(@RequestParam(defaultValue = "1" ) int currentPage ,
                            @RequestParam(defaultValue = "10") int pageSize)
    {
       R r= sevice.findAll(currentPage,pageSize);
     return r;
    }
    //查找所有志愿者信息
    @GetMapping("select")
    public  R selectVolunteer(@RequestParam(defaultValue = "1") int currentPage ,
                              @RequestParam(defaultValue = "10") int pageSize ,
                              @RequestParam(required = false) String volunteerName)
    {
        R r = sevice.selectAll(currentPage,pageSize,volunteerName);
        return r;

    }
    //根据id查询志愿者相关信息
    @GetMapping("getVolunteer")
    public  R selectVolunteer(@RequestParam String volunteersId)
    {
        R r = sevice.selectAll(volunteersId);
        return r;

    }
    @PostMapping("refuse")
    public R refuseVolunteer(@RequestBody Volunteer volunteer){
        int i = sevice.refuseById(volunteer.getVolunteersId(),volunteer.getObjection());
        if (i== 0){
            return R.empty("数据为空",null);
        }else if(i<0){
            return R.error("错误",null);
        }else {
            return R.successful("查询成功", null);
        }

    }
    @PostMapping("pass")
    public R passVolunteer(@RequestBody Volunteer volunteer){
        int i = sevice.passVolunteerById(volunteer.getVolunteersId());
        if (i == 0){
            return R.empty("数据为空",null);
        }else if(i<0){
            return R.error("错误",null);
        }else {
            return R.successful("查询成功", null);
        }
    }

    // 注册老人的账号
    @PostMapping("registerElderly")
    public R registerElderly(@RequestBody Elderly elderly) {
        R r = sevice.registerElderly(elderly);
        return r;
    }
    @GetMapping("search")
    public  R searchElderly(
                            @RequestParam(required = false ,value = "query") String query,
                            @RequestParam(value = "searchType",required = false) String type){
        if(query.isEmpty()&&type.isEmpty()){
            R r = sevice.selectElderlyAll();
            return r;
        }
        if ("idCard".equals(type)){
            R byIdCard = sevice.findByIdCard(query);
            return byIdCard;
        }else if ("name".equals(type)){
            R byName = sevice.findByName(query);
            return byName;
        }else {
            return R.error("查询错误",null);
        }
    }

    // 跳转修改页面密码
    @GetMapping("setPassword")
    public R<Map<String, String>> showPagePassword() {
        HashMap<String, String> mes = new HashMap<>();
        mes.put("message", "showPagePassword");
        return R.successful(null, mes);
    }

    // 设置密码
    @PostMapping("setPassword")
    public R updatePassword(@RequestBody UpdatePassword updatePassword) {
        R r = sevice.setPassword(updatePassword);
        return r;
    }

    // 管理员登录
    @PostMapping("login")
    public R loginAdmin(@RequestBody Admin admin) {
        String communityId = admin.getCommunityId();
        String passWord = admin.getPassWord();
        R logining = sevice.logining(communityId, passWord);
        return logining;
    }

    // 所有用户登录接口
    @GetMapping("login")
    public R loginUser(@RequestParam String userId,
                       @RequestParam String password) {
        if (userId.isEmpty() || password.isEmpty()) {
            return R.empty("账号或密码不能为空！请重新输入！", null);
        } else {
            try {
                if (userService.userLogin(userId, password)) {
                    // 创建token和userType
                    String token = JwtToken.createToken(userId);
                    String userType = null;
                    if (userId.startsWith("E")) {
                        userType = "elderly";
                    } else if (userId.startsWith("J")) {
                        userType = "family";
                    } else if (userId.startsWith("C")) {
                        userType = "community";
                    } else {
                        userType = "volunteers";
                    }

                    LoginUserResponseData loginUserResponseData = userService.selectUserByUserId(userId);

                    loginUserResponseData.setToken(token);
                    loginUserResponseData.setUserType(userType);
                    loginUserResponseData.setUserId(userId);

                    return R.successful("登陆成功！", loginUserResponseData);
                } else {
                    return R.error("账号或密码错误！请重新输入！", null);
                }
            } catch (Exception e) {
               return R.error(e.getMessage(), null);
            }
        }
    }

    // 所有用户注册接口
    @PutMapping("insert")
    public R insertUser(@Validated @RequestBody User user, BindingResult result) {
        if (result.hasErrors()) {
            return R.error("参数为null,不能修改", null);
        } else {
            userService.insert(user);
            return R.successful("注册成功", null);
        }
    }
    @GetMapping("searchRole")
    public R searchPerson(@RequestParam String userId){
        R r = sevice.searchPerson(userId);
        return r;
    }
}
